const app = getApp()
import {
  imageUrl
} from "../../utils/imageutil.js"
Page({
  data: {
    imageUrl: imageUrl,
    customItem: '请选择',
    lat: '',
    lng: '',
    area_code: '',
    user_name: '',
    tel: '',
    is_default: 2,
    address: '',
    area: '',
    id: "",
    doorplate: '',
    switchChecked: false,

    user_id:'',
    add_type:1,

    type: 1,
    province: '',
    city: '',
    region: '',
    street: '',
    province_num: 0,
    city_num: 0,
    region_num: 0,
    street_num: 0,
    value: [0, 0, 0, 0],
    range_list: [],
    province_list: [],
    city_list: [],
    region_list: [],
    street_list: [],

  },
  onLoad: function (options) {
    if(options.user_id){
      this.setData({
        user_id:options.user_id,
        add_type:2
      })
    }
    if (options.id) {
      this.setData({
        id: options.id
      })
      wx.setNavigationBarTitle({
        title: '编辑服务地址'
      })
      this.getMyAddressInfo()
    }else{
      this.citylinkage()
    }
  },
  getMyAddressInfo() {
    app.globalData.post({
      data: {
        do: 'MyAddressInfo',
        id: this.data.id
      }
    }).then(res => {
      let data = res.data
      this.setData({
        address: data.address,
        is_default: data.is_default,
        tel: data.tel,
        user_name: data.user_name,
        doorplate: data.doorplate,
        area: data.area.join(','),
        lat: data.lat,
        lng: data.lng,
        area_code: data.area_code,
      })
      let area_list = data.area
      this.setData({
        switchChecked: this.data.is_default == 1 ? true : false
      })
      this.citylinkage().then(type=>{
        if(data.isuse!=1){
          return
        }
        if(type==1){
          this.data.province_list.forEach((v,i)=>{
            if(v.CRI_NAME==area_list[0]){
              this.setData({
                province_num:i,
                province:v.CRI_CODE,
                value:[this.data.province_num,this.data.city_num,this.data.region_num,this.data.street_num],
              })
            }
          })
          this.setData({
            type:2
          })
          this.citylinkage().then(type=>{
            this.data.city_list.forEach((v,i)=>{
              if(v.CRI_NAME==area_list[1]){
                this.setData({
                  city_num:i,
                  city:v.CRI_CODE,
                  value:[this.data.province_num,this.data.city_num,this.data.region_num,this.data.street_num],
                })
              }
            })
            this.setData({
              type:3
            })
            this.citylinkage().then(type=>{
              this.data.region_list.forEach((v,i)=>{
                if(v.CRI_NAME==area_list[2]){
                  this.setData({
                    region_num:i,
                    region:v.CRI_CODE,
                    value:[this.data.province_num,this.data.city_num,this.data.region_num,this.data.street_num],
                  })
                }
              })
              this.setData({
                type:4
              })
              this.citylinkage().then(type=>{
                this.data.street_list.forEach((v,i)=>{
                  if(v.CRI_NAME==area_list[3]){
                    this.setData({
                      street_num:i,
                      street:v.CRI_CODE,
                      value:[this.data.province_num,this.data.city_num,this.data.region_num,i],
                    })
                  }
                })
              })
            })
          })
        }
      })
    })
  },
  changeuser_name(e) {
    this.setData({
      user_name: e.detail.value
    })
  },
  changeuser_tel(e) {
    this.setData({
      tel: e.detail.value
    })
  },
  changeuser_doorplate(e) {
    this.setData({
      doorplate: e.detail.value
    })
  },
  AddAddress() {
    if (this.data.user_name == '') {
      wx.showToast({
        title: '请填写联系人！',
        icon: "none"
      })
      return false;
    }
    if (!(/^1[3456789]\d{9}$/.test(this.data.tel))) {
      wx.showToast({
        title: '手机号码有误，请重填',
        icon: "none"
      })
      return false;
    }
    if (this.data.address == '') {
      wx.showToast({
        title: '请选择详细地址！',
        icon: "none"
      })
      return false;
    }
    if (this.data.doorplate == '') {
      wx.showToast({
        title: '请填写门牌号！',
        icon: "none"
      })
      return false;
    }
    let domethods = "";
    if (this.data.id) {
      domethods = "UpdAddress"
    } else {
      domethods = "AddAddress"
    }
    if(!this.data.province){
      app.showToast('请选择所在地区')
      return
    }
    if(this.data.value[3]==0&&this.data.street_list.length!=1){
      app.showToast('请选择所在地区街道')
      return
    }
    app.globalData.post({
      data: {
        do: domethods,
        id: this.data.id,
        user_name: this.data.user_name,
        tel: this.data.tel,
        address: this.data.address,
        area: this.data.area,
        lat: this.data.lat,
        lng: this.data.lng,
        area_code: this.data.area_code,
        doorplate: this.data.doorplate,
        is_default: this.data.is_default,
        user_id: this.data.user_id?this.data.user_id : wx.getStorageSync('userInfo').id,
        add_type:this.data.add_type,
        area_ids:this.data.province+','+this.data.city+','+this.data.region+','+ (this.data.street?this.data.street:'0')
      }
    }).then(res => {
      wx.navigateBack({
        delta: 1,
      })
    })
  },
  changeaddress(e) {
    this.setData({
      address: e.detail.value,
    })
  },
  switchChange(e) {
    this.setData({
      is_default: e.detail.value ? '1' : '2'
    })
  },
  
  dingwei: function (e) {
    var a = this;
    wx.chooseLocation({
      success: (e) => {
        var t = e.address.indexOf("区");
        console.log(e.address.substring(t + 1) + e.name), a.setData({
          address: e.address.substring(t + 1) + e.name,
          lat: e.latitude,
          lng: e.longitude
        });
        getApp().reverse_geocoding(e.latitude, e.longitude).then(res => {
          this.setData({
            area_code: res.addressComponent.adcode
          })
        })
      },
      fail: function () {
        wx.getSetting({
          success: function (e) {
            console.log(e), e.authSetting["scope.address"] ? console.log("取消") : wx.showModal({
              title: "提示",
              content: "您拒绝了获取服务地址授权，部分功能无法使用,点击确定重新获取授权。",
              showCancel: !1,
              success: function (e) {
                e.confirm && wx.openSetting({
                  success: function (e) {
                    e.authSetting["scope.address"] && a.dingwei();
                  },
                  fail: function (e) {}
                });
              }
            });
          }
        });
      }
    });
  },
  citylinkage() {
    return new Promise((reslove, reject) => {
      app.globalData.posts(`/api/app/user/sorting/ouyoung_api/gylarea?type=${this.data.type}&province=${this.data.province}&city=${this.data.city}&region=${this.data.region}`).then(res => {
        if (res.data.code != 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          return
        }
        let list = [{
          CRI_NAME: '请选择',
          CRI_CODE: false
        }]
        list.push(...res.data.data)
        if (this.data.type == 1) {
          this.setData({
            province_list: list,
            range_list: [list, this.data.city_list, this.data.region_list, this.data.street_list]
          })
          reslove(1)
        } else if (this.data.type == 2) {
          this.setData({
            city_list: list,
            range_list: [this.data.province_list, list, this.data.region_list, this.data.street_list]
          })
          reslove(2)
        }
        if (this.data.type == 3) {
          this.setData({
            region_list: list,
            range_list: [this.data.province_list, this.data.city_list, list, this.data.street_list]
          })
          reslove(3)
        }
        if (this.data.type == 4) {
          this.setData({
            street_list: list,
            range_list: [this.data.province_list, this.data.city_list, this.data.region_list, list]
          })
          reslove(4)
          return
          if (res.data.data.length == 0) {
            this.setData({
              street_list: [],
              range_list: [this.data.province_list, this.data.city_list, this.data.region_list]
            })
          } else {
            this.setData({
              street_list: list,
              range_list: [this.data.province_list, this.data.city_list, this.data.region_list, list]
            })
            reslove(4)
          }
        }
      })
    }) 
    
  },
  change_picker(e) {
    let list = e.detail.value;
    let range_list = this.data.range_list;
    let flag = true
    list.forEach((v,i)=>{
      if(range_list[i].length!=1&&v==0){
        flag = false
      }
    })
    if(!flag){
      getApp().showToast('请正确选择所在地区')
      return
    }
    this.setData({
      value: list,
      area:range_list[0][list[0]].CRI_NAME+','+range_list[1][list[1]].CRI_NAME+','+range_list[2][list[2]].CRI_NAME+(range_list[3][list[3]].CRI_NAME=='请选择'?'':','+range_list[3][list[3]].CRI_NAME)
    })
  },
  change_column(e) {
    if (e.detail.value == 0) {
      if (e.detail.column == 0) {
        this.setData({
          city_list: [],
          region_list: [],
          street_list: [],
        })
      } else if (e.detail.column == 1) {
        this.setData({
          region_list: [],
          street_list: [],
        })
      } else if (e.detail.column == 2) {
        this.setData({
          street_list: [],
        })
      } else if (e.detail.column == 3) {
        this.setData({

        })
      }
      this.setData({
        range_list: [this.data.province_list, this.data.city_list, this.data.region_list, this.data.street_list]
      })
      return
    }
    let value = this.data.value
    if (e.detail.column == 0) {
      this.setData({
        type: 2,
        province: this.data.province_list[e.detail.value].CRI_CODE,
        city_list: [],
        region_list: [],
        street_list: [],
        value: [e.detail.value, 0, 0, 0]
      })
    } else if (e.detail.column == 1) {
      this.setData({
        type: 3,
        city: this.data.city_list[e.detail.value].CRI_CODE,
        region_list: [],
        street_list: [],
        value: [value[0], e.detail.value, 0, 0]
      })
    } else if (e.detail.column == 2) {
      this.setData({
        type: 4,
        street_list: [],
        region: this.data.region_list[e.detail.value].CRI_CODE,
        value: [value[0], value[1], e.detail.value, 0]
      })
    } else if (e.detail.column == 3) {
      this.setData({
        street: this.data.street_list[e.detail.value].CRI_CODE
      })
    }
    this.citylinkage()
  },
})